import { createRouter, defineEventHandler, useBase } from "h3";
import { GenerateToken, ParseAuth } from "~/utils/auth";
import redisClient from "~/utils/redis";
import { getSignature, checkUserAttention, getOpenId } from "~/utils/wxsdk";
import { version } from "~/package.json";

const BASE_URL = "https://marketing.vrnewg.com/v1";
const router = createRouter();
const IsProd = process.env.NODE_ENV === "production";

const isMoblie = (event) => {
    const userAgent = getHeader(event, "User-Agent");
    return /iPhone|iPad|iPod|Android/i.test(userAgent);
};

const getPlatform = (event) => {
    return isMoblie(event) ? "m" : "p";
};

const getToken = (event) => {
    const token = getHeader(event, "token");
    return token;
};

const GetUID = async (event) => {
    try {
        const token = getToken(event);
        if (token) {
            const userInfo = ParseAuth(token);
            if (IsProd) {
                const saveToken = await redisClient.getVal(
                    `token-${getPlatform(event)}-${userInfo.id}`
                );
                if (token !== saveToken) {
                    setHeader(event, "tokenOut", true);
                    if (process.env.NODE_ENV === "development") {
                        setCookie(event, "ykj_token", "", { path: "/", domain: 'localhost' });
                    } else {
                        setCookie(event, "ykj_token", "", { path: "/", domain: '.ykjhr.com' });
                    }
                    return "";
                }
            }
            return userInfo.id || "";
        }
        return "";
    } catch (e) {
        return "";
    }
};

const Login = async (event) => {
    const body = await readBody(event);
    const res = await ReqPost(event.node.req.url, body, {}, event);
    if (res.code == 200) {
        const token = GenerateToken(res.data);
        if (IsProd)
            await redisClient.setVal(
                `token-${getPlatform(event)}-${res.data.id}`,
                token
            );
        return {
            ...res,
            data: {
                id: res.data.id,
                token,
            },
        };
    }
    return res || {};
};

const ReqPost = async (url, body, headers, event) => {
    const res = await $fetch(BASE_URL + url, {
        method: "post",
        headers: {
            timestamp: event.headers.get("timestamp"),
            randomstr: event.headers.get("randomstr"),
            signature: event.headers.get("signature"),
            "user-agent": event.headers.get("user-agent"),
            "x-forwarded-for": event.headers.get("x-forwarded-for"),
            referer: event.headers.get("referer") || "/",
            ...headers,
        },
        body,
    });
    return res || {};
};

const ReqGet = async (url, query, headers, event) => {
    const res = await $fetch(BASE_URL + url, {
        method: "get",
        headers: {
            timestamp: event.headers.get("timestamp"),
            randomstr: event.headers.get("randomstr"),
            signature: event.headers.get("signature"),
            "user-agent": event.headers.get("user-agent"),
            "x-forwarded-for": event.headers.get("x-forwarded-for"),
            referer: event.headers.get("referer") || "/",
            "Content-Type": "application/json",
            ...headers,
        },
        query,
    });
    return res || {};
};

const ReqUpload = async (url, body, headers, event) => {
    const res = await $fetch(BASE_URL + url, {
        method: "post",
        headers: {
            timestamp: event.headers.get("timestamp"),
            randomstr: event.headers.get("randomstr"),
            signature: event.headers.get("signature"),
            "user-agent": event.headers.get("user-agent"),
            "x-forwarded-for": event.headers.get("x-forwarded-for"),
            referer: event.headers.get("referer") || "/",
            ...headers,
        },
        body,
    });
    return res;
};

router.post("/user/login", defineEventHandler(Login));

router.post(
    "/user/register",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        return await ReqPost("/user/register", body, {}, event);
    })
);

router.get(
    "/user/getTokenInfo",
    defineEventHandler(async (event) => {
        try {
            const token = getToken(event);
            if (token) {
                let userInfo = ParseAuth(token);
                if (IsProd) {
                    const saveToken = await redisClient.getVal(
                        `token-${getPlatform(event)}-${userInfo.id}`
                    );
                    if (token !== saveToken) {
                        setHeader(event, "tokenOut", true);
                        if (process.env.NODE_ENV === "development") {
                            setCookie(event, "ykj_token", "", { path: "/", domain: 'localhost' });
                        } else {
                            setCookie(event, "ykj_token", "", { path: "/", domain: '.ykjhr.com' });
                        }
                        userInfo = {};
                    }
                }
                return { code: 200, data: userInfo, msg: "" };
            }
            return { code: 200, data: {}, msg: "" };
        } catch (e) {
            return { code: 200, data: {}, msg: "" };
        }
    })
);

router.get(
    "/sso/verify",
    defineEventHandler(async (event) => {
        let query = await getQuery(event);
        let ticket = query.ticket || getToken(event);
        let userInfo = ParseAuth(ticket);
        if (IsProd) {
            const saveToken = await redisClient.getVal(
                `token-${getPlatform(event)}-${userInfo.id}`
            );
            if (ticket !== saveToken) {
                userInfo = {};
            }
        }
        return { code: 200, data: userInfo, msg: "" };
    })
);

router.get(
    "/user/loginout",
    defineEventHandler(async (event) => {
        const UID = await GetUID(event);
        await redisClient.delVal(`token-${getPlatform(event)}-${UID}`);
        return { code: 200, data: "ok", msg: "" };
    })
);

router.post(
    "/template/get_template",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        return await ReqPost(
            "/template/get_template",
            { ...body, uid: UID },
            {},
            event
        );
    })
);

router.post(
    "/user/get_user_info",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        return await ReqPost(
            "/user/get_user_info",
            { ...body, uid: UID },
            {},
            event
        );
    })
);

router.post(
    "/industry/get_list",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        return await ReqPost(
            "/industry/get_list",
            { ...body, uid: UID },
            {},
            event
        );
    })
);

router.post(
    "/user/set_user_info",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        return await ReqPost(
            "/user/set_user_info",
            { ...body, uid: UID },
            {},
            event
        );
    })
);

router.post(
    "/user/get_work_list",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        return await ReqPost(
            "/user/get_work_list",
            { ...body, uid: UID },
            {},
            event
        );
    })
);

router.post(
    "/template/get_in_production",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        return await ReqPost(
            "/template/get_in_production",
            {
                ...body,
                uid: UID,
            },
            {},
            event
        );
    })
);

router.post(
    "/template/get_template_category",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        return await ReqPost(
            "/template/get_template_category",
            {
                ...body,
                uid: UID,
            },
            {},
            event
        );
    })
);

router.post(
    "/user/get_works_square_list",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        return await ReqPost(
            "/user/get_works_square_list",
            {
                ...body,
                uid: UID,
            },
            {},
            event
        );
    })
);

router.post(
    "/pay/vip",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        return await ReqPost("/pay/vip", body, {}, event);
    })
);

router.post(
    "/template/get_template_price",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        return await ReqPost(
            "/template/get_template_price",
            {
                ...body,
                uid: UID,
            },
            {},
            event
        );
    })
);

router.post(
    "/pay/check_status",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        return await ReqPost("/pay/check_status", body, {}, event);
    })
);

router.post(
    "/pay/template",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        const headers = event.headers;
        return await ReqPost(
            "/pay/template",
            {
                ...body,
                user_id: UID,
            },
            {
                "user-agent": headers.get("user-agent"),
            },
            event
        );
    })
);

router.post(
    "/pay/get_order_status",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        return await ReqPost("/pay/get_order_status", body, {}, event);
    })
);

router.post(
    "/user/del_work",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        return await ReqPost(
            "/user/del_work",
            { ...body, uid: UID },
            {},
            event
        );
    })
);

router.post(
    "/user/del_in_production",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        return await ReqPost(
            "/user/del_in_production",
            { ...body, uid: UID },
            {},
            event
        );
    })
);

router.post(
    "/template/page_view",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        return await ReqPost("/template/page_view", body, {}, event);
    })
);

router.post(
    "/template/get_page_view",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        return await ReqPost("/template/get_page_view", body, {}, event);
    })
);

router.post(
    "/user/work_square_page_view",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        return await ReqPost("/user/work_square_page_view", body, {}, event);
    })
);

router.post(
    "/user/get_work_square_page_view",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        return await ReqPost(
            "/user/get_work_square_page_view",
            body,
            {},
            event
        );
    })
);

router.post(
    "/template/get_template_list",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        return await ReqPost("/template/get_template_list", body, {}, event);
    })
);

router.post(
    "/user/forget_password",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        return await ReqPost("/user/forget_password", body, {}, event);
    })
);

router.post(
    "/user/change_password",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        return await ReqPost("/user/change_password", body, {}, event);
    })
);

router.post(
    "/user/change_phone",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        return await ReqPost("/user/change_phone", body, {}, event);
    })
);

router.post(
    "/pay/get_vip_info",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        return await ReqPost(
            "/pay/get_vip_info",
            { ...body, user_id: UID },
            {},
            event
        );
    })
);

router.post(
    "/user/code",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        return await ReqPost("/user/code", body, {}, event);
    })
);

router.post("/user/mobilelogin", defineEventHandler(Login));

router.post(
    "/pay/getVipBuyStatus",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        return await ReqPost(
            "/pay/getVipBuyStatus",
            { ...body, uid: UID },
            {},
            event
        );
    })
);

router.post(
    "/pay/getOrderList",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        return await ReqPost(
            "/pay/getOrderList",
            { ...body, uid: UID },
            {},
            event
        );
    })
);

router.post(
    "/pay/getWorkOrderList",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        return await ReqPost(
            "/pay/getWorkOrderList",
            { ...body, uid: UID },
            {},
            event
        );
    })
);

router.post(
    "/pay/getBuyTemplates",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        return await ReqPost(
            "/pay/getBuyTemplates",
            { ...body, uid: UID },
            {},
            event
        );
    })
);

router.post(
    "/pay/getBuyWorks",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        return await ReqPost(
            "/pay/getBuyWorks",
            { ...body, uid: UID },
            {},
            event
        );
    })
);

router.post(
    "/examcase/get_examcase",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        return await ReqPost("/examcase/get_examcase", body, {}, event);
    })
);

router.post(
    "/examcase/get_examcase_cate",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        return await ReqPost("/examcase/get_examcase_cate", body, {}, event);
    })
);

router.post(
    "/index/case",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        return await ReqPost("/index/case", body, {}, event);
    })
);

router.post(
    "/index/get_new_case",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        return await ReqPost(
            "/index/get_new_case",
            {
                ...body,
                uid: UID,
            },
            {},
            event
        );
    })
);

router.post(
    "/index/template",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        return await ReqPost(
            "/index/template",
            { ...body, uid: UID },
            {},
            event
        );
    })
);

router.post(
    "/index/news",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        return await ReqPost("/index/news", body, {}, event);
    })
);
router.post(
    "/index/work_like",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        return await ReqPost(
            "/index/work_like",
            { ...body, uid: UID },
            {},
            event
        );
    })
);

router.post(
    "/index/work_star",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        return await ReqPost(
            "/index/work_star",
            { ...body, uid: UID },
            {},
            event
        );
    })
);

router.post(
    "/index/del_work_star",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        return await ReqPost(
            "/index/del_work_star",
            { ...body, uid: UID },
            {},
            event
        );
    })
);

router.post(
    "/user/contact",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        return await ReqPost("/user/contact", { ...body, uid: UID }, {}, event);
    })
);

router.get(
    "/news/list",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        return await ReqGet("/news/list", query, {}, event);
    })
);

router.get(
    "/news/detail",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        return await ReqGet("/news/detail", query, {}, event);
    })
);

router.get(
    "/user/getAvatar",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        const UID = await GetUID(event);
        return await ReqGet(
            "/user/getAvatar",
            { ...query, uid: UID },
            {},
            event
        );
    })
);

router.post(
    "/user/changeAvatar",
    defineEventHandler(async (event) => {
        const body = await readFormData(event);
        const UID = await GetUID(event);
        body.append("uid", UID);
        return await ReqUpload("/user/changeAvatar", body, {}, event);
    })
);

router.post(
    "/index/banner",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const res = await ReqPost("/index/banner", body, {}, event);
        return res;
    })
);

router.post(
    "/index/works",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        return await ReqPost("/index/works", { ...body, uid: UID }, {}, event);
    })
);

router.post(
    "/log/agree",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        return await ReqPost(
            "/log/agree",
            { ...body, consumer_id: UID },
            {},
            event
        );
    })
);

router.get(
    "/index/vr",
    defineEventHandler(async (event) => {
        return await ReqGet("/index/vr", {}, {}, event);
    })
);

router.post(
    "/examcase/get_examcase_cate_detail",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        const res = await ReqPost(
            "/examcase/get_examcase_cate_detail",
            {
                ...body,
                uid: UID,
            },
            {},
            event
        );
        return res;
    })
);

router.post(
    "/examcase/apply",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        const res = await ReqPost(
            "/examcase/apply",
            {
                ...body,
                user_id: UID,
            },
            {},
            event
        );
        return res;
    })
);

router.get(
    "/index/num",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        const res = await ReqGet("/index/num", query, {}, event);
        return res;
    })
);

router.post(
    "/index/works_share",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        const res = await ReqPost(
            "/index/works_share",
            {
                ...body,
                uid: UID,
            },
            {},
            event
        );
        return res;
    })
);

router.get(
    "/index/get_works_share",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        return await ReqGet("/index/get_works_share", { ...query }, {}, event);
    })
);

router.post(
    "/index/works_comment",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        const res = await ReqPost(
            "/index/works_comment",
            {
                ...body,
                uid: UID,
            },
            {},
            event
        );
        return res;
    })
);

router.post(
    "/index/case_comment",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        const res = await ReqPost(
            "/index/case_comment",
            {
                ...body,
                uid: UID,
            },
            {},
            event
        );
        return res;
    })
);

router.get(
    "/index/get_works_comment",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        return await ReqGet("/index/get_works_comment", query, {}, event);
    })
);

router.get(
    "/index/get_case_comment",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        return await ReqGet("/index/get_case_comment", query, {}, event);
    })
);

router.get(
    "/index/mid_num",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        const UID = await GetUID(event);
        return await ReqGet(
            "/index/mid_num",
            { ...query, uid: UID },
            {},
            event
        );
    })
);

router.get(
    "/index/like_or_star_list",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        const UID = await GetUID(event);
        return await ReqGet(
            "/index/like_or_star_list",
            { ...query, uid: UID },
            {},
            event
        );
    })
);

router.get(
    "/index/get_score_task_list",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        const UID = await GetUID(event);
        return await ReqGet(
            "/index/get_score_task_list",
            {
                ...query,
                uid: UID,
            },
            {},
            event
        );
    })
);

router.get(
    "/index/set_task_score",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        const UID = await GetUID(event);
        return await ReqGet(
            "/index/set_task_score",
            {
                ...query,
                uid: UID,
            },
            {},
            event
        );
    })
);

router.post(
    "/platform/pageview",
    defineEventHandler(async (event) => {
        return await ReqPost("/platform/pageview", {}, {}, event);
    })
);

router.post(
    "/platform/online",
    defineEventHandler(async (event) => {
        return await ReqPost("/platform/online", {}, {}, event);
    })
);

router.get(
    "/platform/ip",
    defineEventHandler(async (event) => {
        return await ReqGet("/platform/ip", {}, {}, event);
    })
);

router.get(
    "/platform/visitor_log",
    defineEventHandler(async (event) => {
        return await ReqGet("/platform/visitor_log", {}, {}, event);
    })
);

router.post(
    "/examcase/examcase_view",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        return await ReqPost("/examcase/examcase_view", { ...body }, {}, event);
    })
);

router.post(
    "/platform/moduleviews",
    defineEventHandler(async (event) => {
        if (event.headers.get("x-forwarded-for")) {
            const body = await readBody(event);
            const UID = await GetUID(event);
            return await ReqPost(
                "/platform/moduleviews",
                { ...body, uid: UID },
                {},
                event
            );
        }
        return {};
    })
);

router.post(
    "/index/examcase_like",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        return await ReqPost(
            "/index/examcase_like",
            { ...body, uid: UID },
            {},
            event
        );
    })
);

router.post(
    "/index/examcase_star",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        return await ReqPost(
            "/index/examcase_star",
            { ...body, uid: UID },
            {},
            event
        );
    })
);

router.post(
    "/index/sign_in",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        return await ReqPost(
            "/index/sign_in",
            {
                ...body,
                uid: UID,
            },
            {},
            event
        );
    })
);

router.get(
    "/index/sign_in_info",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        const UID = await GetUID(event);
        return await ReqGet(
            "/index/sign_in_info",
            {
                ...query,
                uid: UID,
            },
            {},
            event
        );
    })
);

router.get(
    "/index/get_score_log",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        const UID = await GetUID(event);
        return await ReqGet(
            "/index/get_score_log",
            {
                ...query,
                uid: UID,
            },
            {},
            event
        );
    })
);

router.get(
    "/index/get_friendship_link",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        return await ReqGet(
            "/index/get_friendship_link",
            {
                ...query,
            },
            {},
            event
        );
    })
);

router.get(
    "/index/get_score_lottery",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        return await ReqGet(
            "/index/get_score_lottery",
            {
                ...query,
            },
            {},
            event
        );
    })
);

router.post(
    "/index/set_score_lottery",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        return await ReqPost(
            "/index/set_score_lottery",
            {
                ...body,
                uid: UID,
            },
            {},
            event
        );
    })
);

router.get(
    "/index/get_score_exchange",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        return await ReqGet(
            "/index/get_score_exchange",
            {
                ...query,
            },
            {},
            event
        );
    })
);

router.post(
    "/index/set_score_exchange",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        return await ReqPost(
            "/index/set_score_exchange",
            {
                ...body,
                uid: UID,
            },
            {},
            event
        );
    })
);

router.get(
    "/index/get_avatar_list",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        return await ReqGet(
            "/index/get_avatar_list",
            {
                ...query,
            },
            {},
            event
        );
    })
);

router.get(
    "/index/get_score_exchange_coupons",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        const UID = await GetUID(event);
        return await ReqGet(
            "/index/get_score_exchange_coupons",
            {
                ...query,
                uid: UID,
            },
            {},
            event
        );
    })
);

router.get(
    "/message/index",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        const UID = await GetUID(event);
        return await ReqGet(
            "/message/index",
            {
                ...query,
                uid: UID,
            },
            {},
            event
        );
    })
);

router.get(
    "/message/has_new",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        const UID = await GetUID(event);
        return await ReqGet(
            "/message/has_new",
            {
                ...query,
                uid: UID,
            },
            {},
            event
        );
    })
);

router.post(
    "/message/is_read",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        return await ReqPost(
            "/message/is_read",
            {
                ...body,
                uid: UID,
            },
            {},
            event
        );
    })
);

router.get(
    "/index/random_template",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        return await ReqGet(
            "/index/random_template",
            {
                ...query,
            },
            {},
            event
        );
    })
);

router.get(
    "/index/random_work",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        return await ReqGet(
            "/index/random_work",
            {
                ...query,
            },
            {},
            event
        );
    })
);

router.get(
    "/index/random_news",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        return await ReqGet(
            "/index/random_news",
            {
                ...query,
            },
            {},
            event
        );
    })
);

router.post(
    "/comment_reply/reply",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        return await ReqPost(
            "/comment_reply/reply",
            {
                ...body,
                uid: UID,
            },
            {},
            event
        );
    })
);

router.get(
    "/comment_reply/get_reply",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        return await ReqGet(
            "/comment_reply/get_reply",
            {
                ...query,
            },
            {},
            event
        );
    })
);

router.post(
    "/index/del_works_comment",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        return await ReqPost(
            "/index/del_works_comment",
            {
                ...body,
                uid: UID,
            },
            {},
            event
        );
    })
);

router.post(
    "/index/del_case_comment",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        return await ReqPost(
            "/index/del_case_comment",
            {
                ...body,
                uid: UID,
            },
            {},
            event
        );
    })
);

router.get(
    "/index/get_edit_version",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        return await ReqGet(
            "/index/get_edit_version",
            {
                ...query,
            },
            {},
            event
        );
    })
);

router.get(
    "/works_square/top",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        const UID = await GetUID(event);
        return await ReqGet(
            "/works_square/top",
            {
                ...query,
                uid: UID,
            },
            {},
            event
        );
    })
);

router.get(
    "/works_square/hot",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        const UID = await GetUID(event);
        return await ReqGet(
            "/works_square/hot",
            {
                ...query,
                uid: UID,
            },
            {},
            event
        );
    })
);

router.get(
    "/works_square/other",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        const UID = await GetUID(event);
        return await ReqGet(
            "/works_square/other",
            {
                ...query,
                uid: UID,
            },
            {},
            event
        );
    })
);

router.get(
    "/works_square/academy",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        const UID = await GetUID(event);
        return await ReqGet(
            "/works_square/academy",
            {
                ...query,
                uid: UID,
            },
            {},
            event
        );
    })
);

router.get(
    "/works_square/list",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        const UID = await GetUID(event);
        return await ReqGet(
            "/works_square/list",
            {
                ...query,
                uid: UID,
            },
            {},
            event
        );
    })
);

router.get(
    "/works_square/famous",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        const UID = await GetUID(event);
        return await ReqGet(
            "/works_square/famous",
            {
                ...query,
                uid: UID,
            },
            {},
            event
        );
    })
);

router.get(
    "/index/get_live_setting",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        return await ReqGet(
            "/index/get_live_setting",
            {
                ...query,
            },
            {},
            event
        );
    })
);

router.post(
    "/works_square/changeCover",
    defineEventHandler(async (event) => {
        const body = await readFormData(event);
        const UID = await GetUID(event);
        body.append("uid", UID);
        return await ReqUpload("/works_square/changeCover", body, {}, event);
    })
);

router.post(
    "/works_square/updateInfo",
    defineEventHandler(async (event) => {
        const body = await readFormData(event);
        const UID = await GetUID(event);
        body.append("uid", UID);
        return await ReqUpload("/works_square/updateInfo", body, {}, event);
    })
);

router.post(
    "/works_square/changeName",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);

        return await ReqPost(
            "/works_square/changeName",
            {
                ...body,
                uid: UID,
            },
            {},
            event
        );
    })
);

router.post(
    "/works_square/changeIntroduce",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);

        return await ReqPost(
            "/works_square/changeIntroduce",
            {
                ...body,
                uid: UID,
            },
            {},
            event
        );
    })
);

router.get(
    "/get-signature",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        const res = await getSignature(query.url);
        return res;
    })
);

router.get(
    "/ip",
    defineEventHandler(async (event) => {
        return event.headers.get("x-forwarded-for");
    })
);

router.post(
    "/user/init_loginlog",
    defineEventHandler(async (event) => {
        const UID = await GetUID(event);

        return await ReqPost(
            "/user/init_loginlog",
            {
                uid: UID,
            },
            {},
            event
        );
    })
);

router.get(
    "/examcase/get_recommend_case",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        return await ReqGet(
            "/examcase/get_recommend_case",
            {
                ...query,
            },
            {},
            event
        );
    })
);

router.post(
    "/template/get_templates_type",
    defineEventHandler(async (event) => {
        const UID = await GetUID(event);
        const body = await readBody(event);

        return await ReqPost(
            "/template/get_templates_type",
            {
                uid: UID,
                ...body,
            },
            {},
            event
        );
    })
);

router.post(
    "/template/getTemplate",
    defineEventHandler(async (event) => {
        const UID = await GetUID(event);
        const body = await readBody(event);

        return await ReqPost(
            "/template/getTemplate",
            {
                uid: UID,
                ...body,
            },
            {},
            event
        );
    })
);

router.get(
    "/notice-tips",
    defineEventHandler(async (event) => {
        const UID = await GetUID(event);

        if (UID) {
            const value = await redisClient.getVal(`notice-tips-${UID}`);
            if (value) {
                return {
                    code: 200,
                    data: true,
                };
            } else {
                await redisClient.setVal(`notice-tips-${UID}`, 1);
                return {
                    code: 200,
                    data: false,
                };
            }
        } else {
            return {
                code: 500,
                msg: "请先登录",
            };
        }
    })
);

router.get(
    "/search/case",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);

        return await ReqGet(
            "/search/case",
            {
                ...query,
            },
            {},
            event
        );
    })
);

router.get(
    "/search/template",
    defineEventHandler(async (event) => {
        const UID = await GetUID(event);
        const query = await getQuery(event);

        return await ReqGet(
            "/search/template",
            {
                uid: UID,
                ...query,
            },
            {},
            event
        );
    })
);

router.get(
    "/search/news",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);

        return await ReqGet(
            "/search/news",
            {
                ...query,
            },
            {},
            event
        );
    })
);

router.get(
    "/search/works",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);

        return await ReqGet(
            "/search/works",
            {
                ...query,
            },
            {},
            event
        );
    })
);

router.get(
    "/index/get_template_category",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);

        return await ReqGet(
            "/index/get_template_category",
            {
                ...query,
            },
            {},
            event
        );
    })
);

router.post(
    "/index/examcase_share",
    defineEventHandler(async (event) => {
        const UID = await GetUID(event);
        const body = await readBody(event);

        return await ReqPost(
            "/index/examcase_share",
            {
                uid: UID,
                ...body,
            },
            {},
            event
        );
    })
);

router.get(
    "/index/get_account_status",
    defineEventHandler(async (event) => {
        const UID = await GetUID(event);
        const query = await getQuery(event);

        return await ReqGet(
            "/index/get_account_status",
            {
                uid: UID,
                ...query,
            },
            {},
            event
        );
    })
);

router.post(
    "/user/get_personal_pesources",
    defineEventHandler(async (event) => {
        const UID = await GetUID(event);
        const body = await readBody(event);

        return await ReqPost(
            "/user/get_personal_pesources",
            {
                uid: UID,
                ...body,
            },
            {},
            event
        );
    })
);

router.post(
    "/user/del_personal_pesources",
    defineEventHandler(async (event) => {
        const UID = await GetUID(event);
        const body = await readBody(event);

        return await ReqPost(
            "/user/del_personal_pesources",
            {
                uid: UID,
                ...body,
            },
            {},
            event
        );
    })
);

router.post(
    "/user/set_personal_pesources",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);

        return await ReqPost(
            "/user/set_personal_pesources",
            {
                uid: UID,
                ...body,
            },
            {},
            event
        );
    })
);

router.post(
    "/user/upload_avatar",
    defineEventHandler(async (event) => {
        const body = await readFormData(event);

        return await ReqPost("/user/upload_avatar", body, {}, event);
    })
);

router.post(
    "/user/check_file_size",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);

        return await ReqPost(
            "/user/check_file_size",
            {
                ...body,
                uid: UID,
            },
            {},
            event
        );
    })
);

router.post(
    "/user_model/del",
    defineEventHandler(async (event) => {
        const UID = await GetUID(event);
        const body = await readBody(event);

        return await ReqPost(
            "/user_model/del",
            {
                uid: UID,
                ...body,
            },
            {},
            event
        );
    })
);

router.post(
    "/store/get_template_category",
    defineEventHandler(async (event) => {
        const UID = await GetUID(event);
        const body = await readBody(event);

        return await ReqPost(
            "/store/get_template_category",
            {
                uid: UID,
                ...body,
            },
            {},
            event
        );
    })
);

router.post(
    "/user_model/list",
    defineEventHandler(async (event) => {
        const UID = await GetUID(event);
        const body = await readBody(event);

        return await ReqPost(
            "/user_model/list",
            {
                uid: UID,
                ...body,
            },
            {},
            event
        );
    })
);

router.post(
    "/store/list",
    defineEventHandler(async (event) => {
        const UID = await GetUID(event);
        const body = await readBody(event);

        return await ReqPost(
            "/store/list",
            {
                uid: UID,
                ...body,
            },
            {},
            event
        );
    })
);

router.post(
    "/store/detail",
    defineEventHandler(async (event) => {
        const UID = await GetUID(event);
        const body = await readBody(event);

        return await ReqPost(
            "/store/detail",
            {
                uid: UID,
                ...body,
            },
            {},
            event
        );
    })
);

router.post(
    "/user_model/upload",
    defineEventHandler(async (event) => {
        const body = await readFormData(event);
        const UID = await GetUID(event);
        body.append("uid", UID);

        return await ReqPost("/user_model/upload", body, {}, event);
    })
);

router.post(
    "/comment_reply/case_reply",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        return await ReqPost(
            "/comment_reply/case_reply",
            {
                ...body,
                uid: UID,
            },
            {},
            event
        );
    })
);

router.get(
    "/comment_reply/get_case_reply",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        return await ReqGet(
            "/comment_reply/get_case_reply",
            {
                ...query,
            },
            {},
            event
        );
    })
);

router.get(
    "/pay/check_work_authority",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        const UID = await GetUID(event);
        return await ReqGet(
            "/pay/check_work_authority",
            {
                ...query,
                uid: UID,
            },
            {},
            event
        );
    })
);

router.post(
    "/pay/work",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        const res = await ReqPost(
            "/pay/work",
            {
                ...body,
                uid: UID,
            },
            {},
            event
        );
        return res;
    })
);

router.post(
    "/pay/store",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        const res = await ReqPost(
            "/pay/store",
            {
                ...body,
                uid: UID,
            },
            {},
            event
        );
        return res;
    })
);

router.post(
    "/pay/store_exchange",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        const res = await ReqPost(
            "/pay/store_exchange",
            {
                ...body,
                uid: UID,
            },
            {},
            event
        );
        return res;
    })
);

router.post(
    "/pay/store_order_pay",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        const res = await ReqPost(
            "/pay/store_order_pay",
            {
                ...body,
                uid: UID,
            },
            {},
            event
        );
        return res;
    })
);

router.post(
    "/pay/ticket_order_pay",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        const res = await ReqPost(
            "/pay/ticket_order_pay",
            {
                ...body,
                uid: UID,
            },
            {},
            event
        );
        return res;
    })
);

router.get(
    "/index/get_crowd_qrcode",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        return await ReqGet("/index/get_crowd_qrcode", { ...query }, {}, event);
    })
);

router.post(
    "/cart/add",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        const res = await ReqPost(
            "/cart/add",
            {
                ...body,
                uid: UID,
            },
            {},
            event
        );
        return res;
    })
);

router.post(
    "/cart/del",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        const res = await ReqPost(
            "/cart/del",
            {
                ...body,
                uid: UID,
            },
            {},
            event
        );
        return res;
    })
);

router.post(
    "/pay/free_exchange",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        const res = await ReqPost(
            "/pay/free_exchange",
            {
                ...body,
                uid: UID,
            },
            {},
            event
        );
        return res;
    })
);

router.get(
    "/cart/list",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        const UID = await GetUID(event);
        return await ReqGet("/cart/list", { ...query, uid: UID }, {}, event);
    })
);

router.post(
    "/user/user_residence_time",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const res = await ReqPost("/user/user_residence_time", body, {}, event);
        return res;
    })
);

router.get("/pay/get_customized_price",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        return await ReqGet("/pay/get_customized_price", query, {}, event);
    })
);

router.get("/works_square/user_list",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        const UID = await GetUID(event);
        return await ReqGet("/works_square/user_list", { ...query, uid: UID }, {}, event);
    })
);

router.get("/user/homepage",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        const UID = await GetUID(event);
        return await ReqGet("/user/homepage", { ...query, uid: UID }, {}, event);
    }))

router.get("/user/my_homepage",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        const UID = await GetUID(event);
        return await ReqGet("/user/my_homepage", { ...query, uid: UID }, {}, event);
    }))

router.post(
    "/homepage/follow",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        const res = await ReqPost(
            "/homepage/follow",
            {
                ...body,
                uid: UID,
            },
            {},
            event
        );
        return res;
    })
);

router.post(
    "/homepage/unfollow",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        const res = await ReqPost(
            "/homepage/unfollow",
            {
                ...body,
                uid: UID,
            },
            {},
            event
        );
        return res;
    })
);

router.get("/dialogue/info",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        const UID = await GetUID(event);
        return await ReqGet("/dialogue/info", { ...query, uid: UID }, {}, event);
    }))

router.post("/dialogue/send",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        const res = await ReqPost(
            "/dialogue/send",
            {
                ...body,
                uid: UID,
            },
            {},
            event
        );
        return res;
    }))

router.get("/dialogue/history",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        const UID = await GetUID(event);
        return await ReqGet("/dialogue/history", { ...query, uid: UID }, {}, event);
    }))

router.get("/homepage/detail_list",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        return await ReqGet("/homepage/detail_list", query, {}, event);
    }))

router.get("/homepage/user_detail_list",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        const UID = await GetUID(event);
        return await ReqGet("/homepage/user_detail_list", { ...query, uid: UID }, {}, event);
    }))


router.get("/works_square/my_share_list",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        const UID = await GetUID(event);
        return await ReqGet("/works_square/my_share_list", { ...query, uid: UID }, {}, event);
    }))

router.get("/works_square/my_list",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        const UID = await GetUID(event);
        return await ReqGet("/works_square/my_list", { ...query, uid: UID }, {}, event);
    }))

router.get("/works_square/my_star_list",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        const UID = await GetUID(event);
        return await ReqGet("/works_square/my_star_list", { ...query, uid: UID }, {}, event);
    }))

router.get("/works_square/my_like_list",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        const UID = await GetUID(event);
        return await ReqGet("/works_square/my_like_list", { ...query, uid: UID }, {}, event);
    }))

router.post("/index/get_update_case",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);
        return await ReqPost("/index/get_update_case", { ...body, uid: UID }, {}, event);
    }))

router.get("/works_square/update_list",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        const UID = await GetUID(event);
        return await ReqGet("/works_square/update_list", { ...query, uid: UID }, {}, event);
    }))

router.get("/message/friend_list",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        const UID = await GetUID(event);
        return await ReqGet("/message/friend_list", { ...query, uid: UID }, {}, event);
    }))

router.get("/ticket/list",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        const UID = await GetUID(event);
        return await ReqGet("/ticket/list", { ...query, uid: UID }, {}, event);
    }))

router.get("/ticket/detail",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        const UID = await GetUID(event);

        return await ReqGet("/ticket/detail", { ...query, uid: UID }, {}, event);
    }))


router.post("/ticket/share",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);

        return await ReqPost("/ticket/share", { ...body, uid: UID }, {}, event);
    }))

router.post("/ticket/cancel_share",
    defineEventHandler(async (event) => {
        const body = await readBody(event);
        const UID = await GetUID(event);

        return await ReqPost("/ticket/cancel_share", { ...body, uid: UID }, {}, event);
    }))


router.get("/check_user_attention",
    defineEventHandler(async (event) => {
        const query = await getQuery(event);
        const appid = query.appid;
        const secret = query.secret;
        const openid = query.openid;

        return await checkUserAttention(appid, secret, openid);
    }))

router.get("/get_openid", defineEventHandler(async (event) => {
    const query = await getQuery(event);
    const appid = query.appid;
    const secret = query.secret;
    const code = query.code;

    return await getOpenId(appid, secret, code);
}));

router.get("/ticket/share_info", defineEventHandler(async (event) => {
    const query = await getQuery(event);

    return await ReqGet("/ticket/share_info", { ...query }, {}, event);
}));

router.post("/ticket/receive_share", defineEventHandler(async (event) => {
    const body = await readBody(event);
    const UID = await GetUID(event);

    return await ReqPost("/ticket/receive_share", { ...body, uid: UID }, {}, event);
}));

router.get("/knowledge_videos/list", defineEventHandler(async (event) => {
    const query = await getQuery(event);

    return await ReqGet("/knowledge_videos/list", { ...query }, {}, event);
}));

router.post("/knowledge_videos/page_view", defineEventHandler(async (event) => {
    const body = await readBody(event);

    return await ReqPost("/knowledge_videos/page_view", { ...body }, {}, event);
}));

router.get("/version", defineEventHandler(async () => {
    return {
        version
    };
}))

export default useBase("/api", router.handler);
